﻿using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETWJ.Service
{
    public class RoleService : CommonService<Role, RoleDTO, MyDbContext>, IRoleService<Role, RoleDTO>
    {
        public  async Task<int> Update(RoleDTO dto)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                BaseService<Role> bs = new BaseService<Role>(ctx);
                var data =await bs.GetByIdAsync(dto.Id);
                if(data != null)
                {
                    data.Name = dto.Name;
                    
                }
                return await ctx.SaveChangesAsync();
            }
        }

        public async Task<RoleDTO> GetRoleByName(string roleName)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
               var data=await ctx.Set<Role>().Where(e => e.IsDeleted == 0 && e.Name == roleName).AsNoTracking().SingleOrDefaultAsync();
                return AutoMapper.Mapper.Map<RoleDTO>(data);
            }
        }
    }
}
